package models

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"srv/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func MysqlInit() {
	AppConf := appconfig.AppConf.Mysql
	logx.Info(AppConf)
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local", AppConf.User, AppConf.Password, AppConf.Host, AppConf.Port, AppConf.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		logx.Error("数据库链接失败", err)
		panic("数据库链接失败")
	}
	logx.Info("数据库链接成功", dsn)
	err = DB.AutoMigrate(&House{}, &HouseImage{}, &User{})
	if err != nil {
		logx.Error("数据表迁移失败", err)
		panic(err)
	}
	logx.Info("数据表迁移成功")
}
